package cn.com.edu.nju.ecrec.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;

import cn.com.edu.nju.ecrec.dao.UserInfoDao;
import cn.com.edu.nju.ecrec.model.UserInfo;
import cn.com.edu.nju.ecrec.util.JPAFactory;

public class UserInfoDaoImpl implements UserInfoDao {

	private EntityManager em;

	public UserInfoDaoImpl() {
		em = JPAFactory.getManager();
	}

	public void finalize() {
		em.close();
	}

	@Override
	public void commit() {
		em.getTransaction().commit();
	}

	@Override
	public void begin() {
		if (em == null)
			em = JPAFactory.getManager();
		if (!em.isOpen())
			em = JPAFactory.getManager();
		if (!em.getTransaction().isActive())
			em.getTransaction().begin();
	}

	@Override
	public void deleteById(long id) {
		this.begin();
		UserInfo ui = em.find(UserInfo.class, id);
		em.remove(ui);
		this.commit();
	}

	@Override
	public int getCount() {
		this.begin();
		String sql = "select count(ui) from UserInfo ui";
		int count = Integer.parseInt(em.createQuery(sql).getResultList().get(0).toString());
		return count;
	}

	@Override
	public List<UserInfo> getAll() {
		this.begin();
		String sql = "select ui from UserInfo ui";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<Long> getIds() {
		this.begin();
		String sql = "select ui.infoID from UserInfo ui";
		List<Long> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByUserIDs(List<String> userIDs) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.userID in (";
		for(int i = 0;i < userIDs.size()-1; i ++){
			sql += "'"+userIDs.get(0)+"',";
		}
		sql += "'"+userIDs.get(userIDs.size()-1)+"'";
		sql += ")";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByUriName(String uri_name) {
		this.begin();
		String sql = "SELECT distinct ui FROM UserInfo ui where ui.refer_url like '%"+uri_name+"%'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByBrowser(String browser) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.browser='" + browser
				+ "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByBrowserVersion(String browser, String version) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.browser='" + browser
				+ "' and ui.version='" + version + "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByBrowserLan(String browser, String browserLanguage) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.browser='" + browser
				+ "' and ui.browserLanguage='" + browserLanguage + "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByOsInfo(String os) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.os='" + os + "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByUserLan(String os, String userLanguage) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.os='" + os
				+ "' and ui.userLanguage='" + userLanguage + "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByPlatform(String platform) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.platform='"
				+ platform + "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getBySysLan(String platform, String systemLanguage) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.platform='"
				+ platform + "' and ui.systemLanguage='" + systemLanguage + "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByCountry(String country) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.country='" + country
				+ "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByRegion(String country, String region) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.country='" + country
				+ "' and ui.region='" + region + "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<UserInfo> getByCity(String country, String region, String city) {
		this.begin();
		String sql = "SELECT ui FROM UserInfo ui where ui.country='" + country
				+ "' and ui.region='" + region + "' and ui.city='" + city + "'";
		List<UserInfo> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getBrowsers() {
		this.begin();
		String sql = "SELECT distinct ui.browser FROM UserInfo ui";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getVersionsByBrowser(String browser) {
		this.begin();
		String sql = "SELECT distinct ui.version FROM UserInfo ui where ui.browser='"
				+ browser + "'";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getLansByBrowser(String browser) {
		this.begin();
		String sql = "SELECT distinct ui.browserLanguage FROM UserInfo ui where ui.browser='"
				+ browser + "'";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getOs() {
		this.begin();
		String sql = "SELECT distinct ui.os FROM UserInfo ui";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getLansByOs(String os) {
		this.begin();
		String sql = "SELECT distinct ui.userLanguage FROM UserInfo ui where ui.os='"
				+ os + "'";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getPlatforms() {
		this.begin();
		String sql = "SELECT distinct ui.platform FROM UserInfo ui";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getLansByPlatform(String platform) {
		this.begin();
		String sql = "SELECT distinct ui.systemLanguage FROM UserInfo ui where ui.platform='"
				+ platform + "'";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getUserLans() {
		this.begin();
		String sql = "SELECT distinct ui.userLanguage FROM UserInfo ui";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getCountrys() {
		this.begin();
		String sql = "SELECT distinct ui.country FROM UserInfo ui";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getRegionsByCountry(String country) {
		this.begin();
		String sql = "SELECT distinct ui.region FROM UserInfo ui where ui.country='"
				+ country + "'";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

	@Override
	public List<String> getCitysByRegion(String region) {
		this.begin();
		String sql = "SELECT distinct ui.city FROM UserInfo ui where ui.region='"
				+ region + "'";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

}
